﻿using UnityEngine;
using System.Collections;

public class playerMovement : MonoBehaviour {

    bool myIsOnGroundFlag = true;

    float speed = 5;
    float moveThreshold = 0.1f;
    
    private float movex = 0;
    private float iPx = 0;

    short direction = 0;
    float jumpSpeed = 0;

	float startX;
	float startY;

    float horizontalControllerInput = 0;

	// Use this for initialization
	void Start ()
    {
		startX = transform.position.x;
		startY = transform.position.y;
	}
	
	// Update is called once per frame
	void Update ()
    {
        if (Input.touchCount == 1 || Input.GetMouseButtonDown (0) == true)
		{
			Jump ();
		}

		if (Input.touchCount > 1 || Input.GetMouseButtonDown (0) == true)
		{
			transform.position.Set(startX, startY, transform.position.z);
		}

        if (Input.GetKey(KeyCode.A) == true)
        {
            transform.Translate(-10 * Time.deltaTime, 0, 0);
        }
        if (Input.GetKey(KeyCode.D) == true)
        {
            transform.Translate(10 * Time.deltaTime, 0, 0);
        }

        movex = 0;
        iPx = Input.acceleration.x;

        if (Mathf.Abs(iPx) > moveThreshold)
        {
            movex = iPx * speed * Time.deltaTime*5;
            transform.Translate(movex, 0, 0);
        }
        
	}

    void Jump()
    {
        if (myIsOnGroundFlag == true)
        {
            rigidbody.AddForce(Vector2.up * 225);
            myIsOnGroundFlag = false;
        }
    }

    void OnCollisionEnter(Collision aCollision)
    {
        if (aCollision.gameObject.name == "IceTile")
        {
            myIsOnGroundFlag = true;
        }
    }
}
